<?php

namespace IDock\Insurance\TPIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\TPIC\Response\AllTPICResponse;
use IDock\Insurance\TPIC\TPICEnums;
use IDock\Printer\VehicleModelPrinter;
use IDock\Printer\VehiclePrinter;


class VehicleModelResponse extends AllTPICResponse
{

    public function resolve(): Response
    {
        $body = $this->getBody();

        $this->contents = $body;

        if ($body) {
            $xml = simplexml_load_string($body);
            $body = $this->objectToArray($xml);
            $status = $body['head']['status'] ?? '';
            $this->setBody($body);
            if ($status != 'S') {
                $this->setError('获取车型失败');
                return $this;
            }
        }

        return $this;
    }

    public function printerInit(): Response
    {
        $vehicleModelPrinter = new VehicleModelPrinter();
        $modelList = [];
        $body = $this->getBody()['body'] ?? '';
        $modelList = $body['vehicle'] ?? [];
        if (!empty($modelList)) {
            if (!isset($modelList[0])) {
                $tmp = $modelList;
                unset($modelList);
                $modelList[0] = $tmp;
            }
            foreach ($modelList as $datum) {

                $vehiclePrinter = new VehiclePrinter();
                $vehiclePrinter->vehicleModelCode = $datum['modelCode'] ?? '';
                $vehiclePrinter->brandName = $datum['carBrand'] ?? '';
                $vehiclePrinter->vehicleExhaust = $datum['exhaustScale'] ?? '';

                $vehiclePrinter->vehicleName = $datum['printBrandName'] ?? '';
                $vehiclePrinter->vehiclePrice = $datum['purchasePrice'] ?? '';
                $vehiclePrinter->vehiclePriceWithOutTax = $datum['purchasePrice'] ?? '';
                $vehiclePrinter->vehicleSeat = $datum['seat'] ?? '';
                $vehiclePrinter->vehicleYear = $datum['carYear'] ??'';

                $vehiclePrinter->familyName = $datum['farmilyName'] ?? '';
                $vehiclePrinter->power = $datum['power'] ?? '';

                $vehiclePrinter->vehicleTonnage = $datum['tonCount'] ?? '';
                $vehiclePrinter->wholeWeight = $datum['completeKerbMass'] ?? '';
                $typeArr = array_flip(TPICEnums::$energyType);
                unset($typeArr['']);
                $vehiclePrinter->energyTypeCode = $typeArr[$datum['energyType'] ?? ''] ?? '';
                $vehiclePrinter->energyTypeName = IDockEnums::$energyType[$vehiclePrinter->energyTypeCode];
                //$vehiclePrinter->item = $datum;
                $energyType = $datum['energyType'] ?? '';
                if ($energyType == '1' or $energyType == '3') {
                    $vehiclePrinter->isNewEnergy = '1';
                }

                $vehicleModelPrinter->setModelList($vehiclePrinter);
            }
        }


        $this->printer = $vehicleModelPrinter;

        return $this;
    }
}
